package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import controller.IExchangerController;
import fileFiltering.ImageFilter;

public class AddCurrencyDialog extends JDialog implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IExchangerController control;
	private ResourceBundleWrapper resourceBundleWrapper;
	private JTextField currencyNameTextField, currencyCodeTextField,
			iconPathTextField, rateTextField;
	private ImageIcon currencyLogo;
	private JFileChooser fc;
	private JButton okButton, cancelButton, openFileChooser;
	private JLabel currencyLogoLabel, currencyNameLabel, currencyCodeLabel,
			iconPathTextLabel, rateLabel;
	private TitledBorder title;
	private JDialog parent;

	public AddCurrencyDialog(IExchangerController control,
			ResourceBundleWrapper resourceBundleWrapper, JDialog parent) {
		super();
		this.control = control;
		this.resourceBundleWrapper = resourceBundleWrapper;
		this.parent = parent;
		this.setTitle(this.resourceBundleWrapper.getString("modify"));
		this.setSize(600, 200);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.buildContentPane();
		this.setVisible(true);
	}

	public void buildContentPane() {
		JPanel mainPanel = new JPanel(new BorderLayout());
		this.title = BorderFactory.createTitledBorder(
				BorderFactory.createLineBorder(Color.black),
				this.resourceBundleWrapper.getString("addFullText"));
		this.title.setTitleJustification(TitledBorder.LEFT);
		mainPanel.setBorder(title);
		this.openFileChooser = new JButton(
				this.resourceBundleWrapper.getString("browse"));
		this.openFileChooser.addActionListener(this);
		// le "formulaire"
		JPanel formPanel = new JPanel(new GridLayout(4, 1));
			// TextFields
			this.currencyNameTextField = new JTextField(20);
			;
			this.currencyCodeTextField = new JTextField(20);
			;
			this.iconPathTextField = new JTextField(20);
			;
			this.rateTextField = new JTextField(20);
			;
			this.rateTextField.setDocument(new DigitLimited(this.rateTextField));
			// labels
			this.currencyNameLabel = new JLabel(
					this.resourceBundleWrapper.getString("currencyName"));
			this.currencyCodeLabel = new JLabel(
					this.resourceBundleWrapper.getString("currencyCode"));
			this.iconPathTextLabel = new JLabel(
					this.resourceBundleWrapper.getString("logo"));
			this.rateLabel = new JLabel(
					this.resourceBundleWrapper.getString("rate"));
			
			formPanel.add(buildFormField(this.currencyNameLabel,
					this.currencyNameTextField, false));
			formPanel.add(buildFormField(this.currencyCodeLabel,
					this.currencyCodeTextField, false));
			formPanel.add(buildFormField(this.iconPathTextLabel,
					this.iconPathTextField, true));
			formPanel
					.add(buildFormField(this.rateLabel, this.rateTextField, false));
		mainPanel.add(formPanel, BorderLayout.CENTER);
		// les boutons
		JPanel buttonPanel = new JPanel();
		this.okButton = new JButton(this.resourceBundleWrapper.getString("ok"));
		this.cancelButton = new JButton(
				this.resourceBundleWrapper.getString("cancel"));
		this.okButton.addActionListener(this);
		this.cancelButton.addActionListener(this);
		buttonPanel.add(this.okButton);
		buttonPanel.add(this.cancelButton);
		mainPanel.add(buttonPanel, BorderLayout.SOUTH);
		this.getContentPane().add(mainPanel);
	}

	private JPanel buildFormField(JLabel label, JTextField textField,
			boolean containsFileChooser) {
		JPanel formField = new JPanel(new GridLayout(1, 3));
		formField.add(label);
		formField.add(textField);
		if (containsFileChooser) {
			textField.setEditable(false);
			JPanel iconPanel = new JPanel(new BorderLayout());
			this.currencyLogo = new ImageIcon("img/empty.gif");
        	this.currencyLogo.setImage(this.currencyLogo.getImage().getScaledInstance(15, 13, Image.SCALE_DEFAULT));
			this.currencyLogoLabel = new JLabel(this.currencyLogo);
			iconPanel.add(this.currencyLogoLabel, BorderLayout.CENTER);
			iconPanel.add(this.openFileChooser, BorderLayout.EAST);
			formField.add(iconPanel);
		} else
			formField.add(new JPanel());
		return formField;
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		this.title.setTitle(this.resourceBundleWrapper.getString("addFullText"));
		this.currencyNameLabel.setText(this.resourceBundleWrapper.getString("currencyName"));
		this.currencyCodeLabel.setText(this.resourceBundleWrapper.getString("currencyCode"));
		this.iconPathTextLabel.setText(this.resourceBundleWrapper.getString("logo"));
		this.rateLabel.setText(this.resourceBundleWrapper.getString("rate"));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == this.openFileChooser) {
			UIManager.getDefaults().setDefaultLocale(Locale.getDefault());
			JComponent.setDefaultLocale(Locale.getDefault());
			this.fc = new JFileChooser();
			this.fc.setFileFilter(new ImageFilter());
			this.fc.setCurrentDirectory(new File("./img"));
			this.fc.setAcceptAllFileFilterUsed(false);
			int returnVal = fc.showOpenDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File f = fc.getSelectedFile();
				if (f.exists()) {
					this.currencyLogo = new ImageIcon(f.getAbsolutePath());
					this.currencyLogo.setImage(this.currencyLogo.getImage().getScaledInstance(15, 13, Image.SCALE_DEFAULT));
					this.iconPathTextField.setText(f.getAbsolutePath());
					this.currencyLogoLabel.setIcon(currencyLogo);
					repaint();
				}
			}
		} else if (e.getSource() == this.okButton) {
			control.addCurrency(currencyNameTextField.getText(),
					currencyCodeTextField.getText(),
					Double.parseDouble(rateTextField.getText()),
					iconPathTextField.getText());
			if(this.parent != null)
				((CurrenciesTableDialog)this.parent).refresh();
			this.dispose();
		} else if (e.getSource() == this.cancelButton) {
			this.dispose();
		}
	}
}
